package huxibianjie.com.lbscandroid.ui.new_activity;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import huxibianjie.com.lbscandroid.R;
import huxibianjie.com.lbscandroid.base.BaseActivity;
import huxibianjie.com.lbscandroid.task.Callback;
import huxibianjie.com.lbscandroid.task.Task;
import huxibianjie.com.lbscandroid.ui.Service;
import huxibianjie.com.lbscandroid.util.StringUtils;
import huxibianjie.com.lbscandroid.widget.CountdownTextView;

public class ForgetPasswordActivity extends BaseActivity {

    @BindView(R.id.et_phone_number)
    EditText mPhoneNumber;
    @BindView(R.id.del_phone_number)
    ImageView mDelPhoneNumber;
    @BindView(R.id.et_phone_code)
    EditText mPhoneCode;
    @BindView(R.id.get_code)
    CountdownTextView countdownTextView;
    @BindView(R.id.et_pass1)
    EditText mEtPass1;
    @BindView(R.id.et_pass2)
    EditText mEtPass2;


    @Override
    protected int getContentView() {
        return R.layout.activity_forget_password;
    }

    @Override
    protected void initView() {
        initTitle(R.drawable.ic_back, "修改密码");
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {
        mPhoneNumber.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0) {
                    mDelPhoneNumber.setVisibility(View.VISIBLE);
                } else {
                    mDelPhoneNumber.setVisibility(View.INVISIBLE);
                }
            }
        });
    }

    @OnClick({R.id.del_phone_number, R.id.get_code, R.id.btn_update})
    public void onClick(View v) {
        String phone = mPhoneNumber.getText().toString();
        switch (v.getId()) {
            case R.id.del_phone_number:
                mPhoneNumber.setText("");
                break;
            case R.id.get_code:
                if (!StringUtils.isMobile(phone)) {
                    showToast("请输入正确的手机号");
                    return;
                }
                showLoading();
                Task.create(Service.class).sentCode(phone).enqueue(new huxibianjie.com.lbscandroid.task.Callback<Map<String, String>>() {
                    @Override
                    public void onSuccess(@Nullable Map<String, String> map) {
                        cancelLoading();
                        showToast("验证码已发送,请注意查收");
                        countdownTextView.start();
                    }

                    @Override
                    public void onFailure(@NonNull String error) {
                        cancelLoading();
                        showToast(error);
                    }
                });
                break;
            case R.id.btn_update:
                String code = mPhoneCode.getText().toString().trim();
                String pass1 = mEtPass1.getText().toString().trim();
                String pass2 = mEtPass2.getText().toString().trim();

                if (!StringUtils.isMobile(phone)) {
                    showToast("请输入正确的手机号");
                    mPhoneNumber.requestFocus();
                    mPhoneNumber.setSelection(mPhoneNumber.getText().toString().length());
                    return;
                }
                if (StringUtils.isEmpty(code)) {
                    showToast("请输入正确的验证码");
                    mPhoneCode.requestFocus();
                    mPhoneCode.setSelection(mPhoneCode.getText().toString().length());
                    return;
                }
                if (!isLetterDigit(pass2)) {
                    showToast("密码格式不正确，请重新输入。");
                    mEtPass1.setText("");
                    mEtPass2.setText("");
                    mEtPass1.requestFocus();
                    return;
                }
                if (!pass1.equals(pass2)) {
                    showToast(R.string.froget_password_toast);
                    return;
                }
                showLoading();
                Task.create(Service.class).updatePassword(phone, code, pass2).enqueue(new Callback<String>() {
                    @Override
                    public void onSuccess(@Nullable String data) {
                        cancelLoading();
                        if (data != null) {
                            showToast("密码修改成功");
                            finish();
                        } else {
                            showToast("密码修改失败");
                        }
                    }

                    @Override
                    public void onFailure(@NonNull String error) {
                        cancelLoading();
                        showToast(error);
                    }
                });
                break;

        }

    }

    /**
     * 规则：至少包含大小写字母及数字中的两种
     */
    private boolean isLetterDigit(String str) {
        return str.matches("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$");
    }
}


